/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package com.googlecode.ivybeans.module.options;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;
import javax.swing.JComponent;
import org.netbeans.spi.options.OptionsPanelController;
import org.openide.util.Exceptions;
import org.openide.util.HelpCtx;
import org.openide.util.Lookup;

/**
 *
 * @author lforet
 */
public class IvyOptionsPanelController extends OptionsPanelController {

    PropertyChangeSupport pcs = new PropertyChangeSupport(this);
    public static Logger LOG = Logger.getLogger("com.googlecode.ivybeans.module");
    private IvyAdvancedOption ivyOptions;
    private IvyOptionPanel ivyPanel;
    
    public IvyOptionsPanelController(IvyAdvancedOption ivyOptions) {
        this.ivyOptions = ivyOptions;
    }
    
    @Override
    public void update() {
        try {
            ivyPanel.setIvyHome(ivyOptions.getIvyHome().getCanonicalPath());
            final File ivyExtensionsDir = ivyOptions.getIvyExtensionsDir();
            ivyPanel.setIvyExtensionDir(ivyExtensionsDir == null ? "" : ivyExtensionsDir.getCanonicalPath());
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }
    }

    @Override
    public void applyChanges() {
        ivyOptions.setIvyHome(ivyPanel.getSelectedIvyHome());
        String extensions = ivyPanel.getIvyExtensionDir();
        File extensionDir = null;
        if(extensions != null && !extensions.isEmpty()) {
            extensionDir = new File(extensions);
        }
        if(extensionDir == null || (extensionDir.exists() && extensionDir.isDirectory())) {
            ivyOptions.setIvyExtensionsDir(extensionDir);
        }
    }

    @Override
    public void cancel() {
    }

    @Override
    public boolean isChanged() {
        boolean isChanged = false;
        try {
            isChanged = !ivyOptions.getIvyHome().getCanonicalPath().equals(ivyPanel.getSelectedIvyHome());
            isChanged |= !ivyOptions.getIvyExtensionsDir().getCanonicalPath().equals(ivyPanel.getIvyExtensionDir());
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
    }
        return isChanged;
    }

    @Override
    public boolean isValid() {
        if(! new File(ivyPanel.getSelectedIvyHome()).exists()) return false;
        final String ivyExtensionDirPath = ivyPanel.getIvyExtensionDir();
        if(! ivyExtensionDirPath.isEmpty()) {
            final File extensionDir = new File(ivyExtensionDirPath);
            if(! extensionDir.exists()) return false;
            if(! extensionDir.isDirectory()) return false;
        }
        return true;
    }

    public void addPropertyChangeListener(PropertyChangeListener l) {
        pcs.addPropertyChangeListener(l);
    }

    @Override
    public JComponent getComponent(Lookup masterLookup) {
        if (ivyPanel == null) {
            ivyPanel = new IvyOptionPanel(ivyOptions);
    }
        return ivyPanel;
    }

    @Override
    public HelpCtx getHelpCtx() {
        return HelpCtx.DEFAULT_HELP;
    }

    @Override
    public void removePropertyChangeListener(PropertyChangeListener l) {
        pcs.removePropertyChangeListener(l);
    }
    }
